package restaurantmanagementsystem;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import java.sql.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newAccount.java
 *
 * Created on Oct 23, 2010, 12:49:43 AM
 */

/**
 *
 * @author edwind
 */
public class newAccount extends javax.swing.JFrame {

    /** Creates new form newAccount */
    public newAccount() {
        super("New Account Information");
        initComponents();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        this.setLocation(windowX, windowY);
        this.setSize(windowSize);
        this.setResizable(false);

        doneButton.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        newAccount = new javax.swing.JPanel();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        retypePasswordLabel = new javax.swing.JLabel();
        staffName = new javax.swing.JLabel();
        staffIDLabel = new javax.swing.JLabel();
        equalLabel1 = new javax.swing.JLabel();
        equalLabel2 = new javax.swing.JLabel();
        equalLabel3 = new javax.swing.JLabel();
        equalLabel4 = new javax.swing.JLabel();
        equalLabel5 = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        passwordField1 = new javax.swing.JPasswordField();
        passwordField2 = new javax.swing.JPasswordField();
        staffNameField = new javax.swing.JTextField();
        staffIDField = new javax.swing.JTextField();
        doneButton = new javax.swing.JLabel();
        cancelButton = new javax.swing.JLabel();
        infoLabel1 = new javax.swing.JLabel();
        infoLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        newAccount.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "New Account", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14), new java.awt.Color(227, 227, 233))); // NOI18N
        newAccount.setOpaque(false);
        newAccount.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        usernameLabel.setForeground(new java.awt.Color(227, 227, 233));
        usernameLabel.setText("Username");
        newAccount.add(usernameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 36, -1, -1));

        passwordLabel.setForeground(new java.awt.Color(227, 227, 233));
        passwordLabel.setText("Password");
        newAccount.add(passwordLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 74, -1, -1));

        retypePasswordLabel.setForeground(new java.awt.Color(227, 227, 233));
        retypePasswordLabel.setText("Retype Password");
        newAccount.add(retypePasswordLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 112, -1, -1));

        staffName.setForeground(new java.awt.Color(227, 227, 233));
        staffName.setText("Staff name");
        newAccount.add(staffName, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 150, -1, -1));

        staffIDLabel.setForeground(new java.awt.Color(227, 227, 233));
        staffIDLabel.setText("Staff ID");
        newAccount.add(staffIDLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(46, 187, 77, -1));

        equalLabel1.setForeground(new java.awt.Color(227, 227, 233));
        equalLabel1.setText(":");
        newAccount.add(equalLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(192, 36, -1, -1));

        equalLabel2.setForeground(new java.awt.Color(227, 227, 233));
        equalLabel2.setText(":");
        newAccount.add(equalLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(192, 74, -1, -1));

        equalLabel3.setForeground(new java.awt.Color(227, 227, 233));
        equalLabel3.setText(":");
        newAccount.add(equalLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(192, 112, -1, -1));

        equalLabel4.setForeground(new java.awt.Color(227, 227, 233));
        equalLabel4.setText(":");
        newAccount.add(equalLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(192, 150, -1, -1));

        equalLabel5.setForeground(new java.awt.Color(227, 227, 233));
        equalLabel5.setText(":");
        newAccount.add(equalLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(192, 187, -1, -1));

        usernameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                usernameFieldKeyReleased(evt);
            }
        });
        newAccount.add(usernameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(229, 35, 179, 20));

        passwordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                passwordField1KeyReleased(evt);
            }
        });
        newAccount.add(passwordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(229, 73, 179, 20));

        passwordField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                passwordField2KeyReleased(evt);
            }
        });
        newAccount.add(passwordField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(229, 111, 179, 20));

        staffNameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                staffNameFieldKeyReleased(evt);
            }
        });
        newAccount.add(staffNameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(229, 149, 179, 20));

        staffIDField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                staffIDFieldKeyReleased(evt);
            }
        });
        newAccount.add(staffIDField, new org.netbeans.lib.awtextra.AbsoluteConstraints(229, 187, 179, 18));

        doneButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tick.png"))); // NOI18N
        doneButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        doneButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                doneButtonMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                doneButtonMouseEntered(evt);
            }
        });
        newAccount.add(doneButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(192, 240, -1, -1));

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        cancelButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cancelButtonMouseEntered(evt);
            }
        });
        newAccount.add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 240, -1, -1));

        infoLabel1.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        infoLabel1.setForeground(new java.awt.Color(127, 223, 187));
        infoLabel1.setText("*4-8 Characters");
        newAccount.add(infoLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 50, -1, -1));

        infoLabel2.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        infoLabel2.setForeground(new java.awt.Color(127, 223, 187));
        infoLabel2.setText("*4-8 Characters");
        newAccount.add(infoLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 90, -1, -1));

        mainPanel.add(newAccount, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 30, 450, 280));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/background.png"))); // NOI18N
        mainPanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void usernameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameFieldKeyReleased
        formComplete();
    }//GEN-LAST:event_usernameFieldKeyReleased

    private void passwordField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordField1KeyReleased
        formComplete();
    }//GEN-LAST:event_passwordField1KeyReleased

    private void passwordField2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordField2KeyReleased
        formComplete();
    }//GEN-LAST:event_passwordField2KeyReleased

    private void staffNameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_staffNameFieldKeyReleased
        formComplete();
    }//GEN-LAST:event_staffNameFieldKeyReleased

    private void staffIDFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_staffIDFieldKeyReleased
        formComplete();
    }//GEN-LAST:event_staffIDFieldKeyReleased

    private void doneButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_doneButtonMouseEntered
        if (!doneButton.isEnabled()){
            doneButton.setToolTipText("Please fill up all fields");
        }
        else{
            doneButton.setToolTipText("Done");
        }
    }//GEN-LAST:event_doneButtonMouseEntered

    private void cancelButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseEntered
        cancelButton.setToolTipText("Cancel");
    }//GEN-LAST:event_cancelButtonMouseEntered

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_cancelButtonMouseClicked

    private void doneButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_doneButtonMouseClicked
        verifyForm();
    }//GEN-LAST:event_doneButtonMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new newAccount().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cancelButton;
    private javax.swing.JLabel doneButton;
    private javax.swing.JLabel equalLabel1;
    private javax.swing.JLabel equalLabel2;
    private javax.swing.JLabel equalLabel3;
    private javax.swing.JLabel equalLabel4;
    private javax.swing.JLabel equalLabel5;
    private javax.swing.JLabel infoLabel1;
    private javax.swing.JLabel infoLabel2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel newAccount;
    private javax.swing.JPasswordField passwordField1;
    private javax.swing.JPasswordField passwordField2;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel retypePasswordLabel;
    private javax.swing.JTextField staffIDField;
    private javax.swing.JLabel staffIDLabel;
    private javax.swing.JLabel staffName;
    private javax.swing.JTextField staffNameField;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables

    public void formComplete(){

        if (((usernameField.getText().trim()).length() > 0)  && //done button can
            ((passwordField1.getText().trim()).length() > 0) && //only be clicked
            ((passwordField2.getText().trim()).length() > 0) && //if all fields
            ((staffNameField.getText().trim()).length() > 0) && //are filled up
            ((staffIDField.getText().trim()).length() > 0)){
            doneButton.setEnabled(true);
        }
        else {
            doneButton.setEnabled(false);
        }
    }

    public void verifyForm(){
        
        String newUsername =  usernameField.getText().replaceAll(" ","");
        String newPassword =  passwordField1.getText().replaceAll(" ","");
        String newPassword2 =  passwordField2.getText().replaceAll(" ","");

        if ((usernameField.getText()).length() < 4 || (usernameField.getText()).length() > 8){
            JOptionPane.showMessageDialog(this,
            "Username must be between 4 to 8 characters.",
            "Warning",//title
            JOptionPane.WARNING_MESSAGE,
            null);
            return;
        }

        if ((passwordField1.getText()).length() < 4 || (passwordField1.getText()).length() > 8){
            JOptionPane.showMessageDialog(this,
            "Username must be between 4 to 8 characters.",
            "Warning",//title
            JOptionPane.WARNING_MESSAGE,
            null);
            return;
        }

        if ((usernameField.getText()).length() != newUsername.length() ||
            (passwordField1.getText()).length() != newPassword.length() ||
            (passwordField2.getText()).length() != newPassword2.length()){

            JOptionPane.showMessageDialog(this,
            "One or more field contains space character",
            "Warning",//title
            JOptionPane.WARNING_MESSAGE,
            null);
            return;
        }


        if (!passwordField1.getText().equals(passwordField2.getText())){

            JOptionPane.showMessageDialog(this,
            "The 2 passwords doesn't match.",
            "Warning",//title
            JOptionPane.WARNING_MESSAGE,
            null);
            return;
        }

        try {
            DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

            Connection con;
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "edwind", "cat200");
            System.out.println("Database Connected");

            Statement stmt = con.createStatement();
            ResultSet rset;

            rset = stmt.executeQuery("SELECT USERNAME FROM USERSINFO");

            while (rset.next()){
                if (usernameField.getText().equals(rset.getString("USERNAME"))){
                    JOptionPane.showMessageDialog(this,
                    "The username already exists.",
                    "Warning",//title
                    JOptionPane.WARNING_MESSAGE,
                    null);
                    return;
                }
            }

            String tempStaffCode = "";
            String tempStaffName = "";
            Boolean found = false;

            rset = stmt.executeQuery("SELECT EMP_ID, EMP_NAME FROM EMPLOYEE");

            while (rset.next()){
                tempStaffCode = rset.getString("EMP_ID");
                tempStaffName = rset.getString("EMP_NAME");

                if (tempStaffName.equals(staffNameField.getText()) && tempStaffCode.equals(staffIDField.getText())){
                    found = true;
                    break;
                }
            }

            if (!found){
                JOptionPane.showMessageDialog(this,
                "Your name/staff code doesn't exist in our staff database.",
                "Warning",//title
                JOptionPane.WARNING_MESSAGE,
                null);
                return;
            }

            int privilege = 0;
            int jobNo = 0;

            rset = stmt.executeQuery("SELECT JOB_NO FROM EMPLOYEE WHERE EMP_ID = '"
                    + staffIDField.getText() + "'");

            while (rset.next()){
                jobNo = rset.getInt("JOB_NO");
            }

            if ((jobNo > 0) && (jobNo < 101)){
                privilege = 1;
            }
            else if((jobNo > 100) && (jobNo < 201))
            {
                privilege = 2;
            }
            else if((jobNo > 200) && (jobNo < 300))
            {
                privilege = 3;
            }
            else {
                JOptionPane.showMessageDialog(this,
                "You are not authorised to create a new account.",
                "Warning",//title
                JOptionPane.WARNING_MESSAGE,
                null);
                return;
            }

            stmt = con.createStatement();
            ResultSet rset2 = stmt.executeQuery("SELECT USER_ID FROM USERSINFO");

            int countID = 1;
            while (rset2.next()){
                countID++;
            }

            String update = "INSERT INTO USERSINFO (USER_ID, USERNAME, PASSWORD, PRIVILEGE, EMP_ID) VALUES ('" + countID +  "', '" + usernameField.getText() + "' , '" + passwordField1.getText() + "', '" + privilege + "', '" + staffIDField.getText() + "')";
            
            System.out.println(update);
            stmt.executeUpdate(update);

            stmt.close();
            con.close();

            JOptionPane.showMessageDialog(this,
            "   New account created!",
            "Succes!",//title
            JOptionPane.INFORMATION_MESSAGE,
            null);

            dispose();
        }
        catch (Exception e){
            System.out.println("error");
        }



    }
}
